// $Id: DomainEntity.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 NourY Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Salamanca.DataAccess;
using Salamanca.DataAccess.EnterpriseLibrary.Caching;
using Northwind;
using Northwind.DomainModel;
using Northwind.DomainModel.EnterpriseLibrary;

namespace Salamanca.NUnit.DataAccess.Northwind.EnterpriseLibrary
{

    public abstract class EnterpriseLibraryDomainEntityBase:
        DomainEntityBase
    {
        [ClassInitialize]
        public void FixtureSetUp(TestContext testContext)
        {
        }

        [ClassCleanup]
        public void FixtureTearDown()
        {
        }

        [TestInitialize]
        public void SetUp()
        {
            _Db=CreateDb();
            _Cache=CreateCacheManager();

            Mappers=global::Northwind.DomainModel.EnterpriseLibrary.Mappers.CreateDataMapperCollection(_Db.Database, _Cache);
            switch (_Db.DatabaseType)
            {
            case NorthwindDatabaseType.Access:
            //case NorthwindDatabaseType.SqlCompact:
                Mappers.PersistenceContextProvider=new Salamanca.DataAccess.EnterpriseLibrary.Data.PersistenceContextProvider(_Db.Database);
                break;
            }
        }

        [TestCleanup]
        public void TearDown()
        {
            Customer.IdentityMap.Clear();
            Employee.IdentityMap.Clear();
            Order.IdentityMap.Clear();
            OrderDetails.IdentityMap.Clear();
            if (_Cache!=null)
                _Cache.Clear();

            _Db.Dispose();
        }

        protected abstract NorthwindDb CreateDb();
        protected abstract ICacheManager CreateCacheManager();

        private NorthwindDb _Db;
        private ICacheManager _Cache;
    }

    [TestClass]
    public class AccessDomainEntityFixture:
        EnterpriseLibraryDomainEntityBase
    {
        protected override NorthwindDb CreateDb()
        {
            return new NorthwindDb(NorthwindDatabaseType.Access);
        }

        protected override ICacheManager CreateCacheManager()
        {
            return new CacheManagerAdapter(Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager());
        }
    }

    [TestClass]
    public class AccessNoCacheDomainEntityFixture:
        AccessDomainEntityFixture
    {
        protected override ICacheManager CreateCacheManager()
        {
            return new NoCacheManager();
        }

    }

    [TestClass]
    public class SqlClientDomainEntityFixture:
        EnterpriseLibraryDomainEntityBase
    {
        protected override NorthwindDb CreateDb()
        {
            return new NorthwindDb(NorthwindDatabaseType.SqlClient);
        }

        protected override ICacheManager CreateCacheManager()
        {
            return new CacheManagerAdapter(Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager());
        }
    }

    [TestClass]
    public class SqlClientNoCacheDomainEntityFixture:
        SqlClientDomainEntityFixture
    {
        protected override ICacheManager CreateCacheManager()
        {
            return new NoCacheManager();
        }

    }

    //[TestClass]
    //[Category("SqlCompact")]
    //public class SqlCompactDomainEntityFixture:
    //    EnterpriseLibraryDomainEntityBase
    //{
    //    protected override NorthwindDb CreateDb()
    //    {
    //        return new NorthwindDb(NorthwindDatabaseType.SqlCompact);
    //    }

    //    protected override ICacheManager CreateCacheManager()
    //    {
    //        return new CacheManagerAdapter(Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager());
    //    }
    //}

    //[TestClass]
    //[Category("SqlCompact")]
    //public class SqlCompactNoCacheDomainEntityFixture:
    //    SqlCompactDomainEntityFixture
    //{
    //    protected override ICacheManager CreateCacheManager()
    //    {
    //        return new NoCacheManager();
    //    }

    //}

    [TestClass]
    public class SqLiteDomainEntityFixture:
        EnterpriseLibraryDomainEntityBase
    {
        protected override NorthwindDb CreateDb()
        {
            return new NorthwindDb(NorthwindDatabaseType.SQLite);
        }

        protected override ICacheManager CreateCacheManager()
        {
            return new CacheManagerAdapter(Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory.GetCacheManager());
        }
    }

    [TestClass]
    public class SqlLiteNoCacheDomainEntityFixture:
        SqLiteDomainEntityFixture
    {
        protected override ICacheManager CreateCacheManager()
        {
            return new NoCacheManager();
        }

    }
}
